package org.yu.register;

import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Tier;
import net.minecraft.world.item.Tiers;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraftforge.client.model.generators.BlockStateProvider;
import net.minecraftforge.client.model.generators.ConfiguredModel;
import net.minecraftforge.client.model.generators.ModelFile;
import org.lwjgl.system.CallbackI;
import org.yu.Yu;
import org.yu.api.blocks.data.BlockFacingFor4Side;
import org.yu.api.register.BlockEntityTypeHolder;
import org.yu.api.register.BlockHolder;
import org.yu.api.register.LazyRegistry;
import org.yu.api.register.MenuHolder;
import org.yu.api.util.ModelUtil;
import org.yu.register.machine.*;

import java.util.function.BiConsumer;

public class MachineRegistry {
    private static final LazyRegistry registry= Yu.LAZYREGISTRY;
    public static final BlockHolder<ImpactAltar> impactAltar=registry.block("impact_altar",ImpactAltar::new)
            .axe().pickaxe().hoe().shovel()
            .tier(Tiers.STONE)
            .propertie(properties -> properties.noOcclusion().lightLevel(state->3))
            .name("zh_cn","冲击祭坛")
            .build();
    public static final BlockEntityTypeHolder<ImpactAltarBlockEntity> impactAltarBlockEntity=registry.blockEntity("impact_altar",ImpactAltarBlockEntity::new)
            .withBlocks(impactAltar)
            .build();
    public final static BlockHolder<StoveBlock> stove=registry.block("stove",StoveBlock::new)
            .propertie(properties -> properties.lightLevel(state->{
                if(state.getValue(StoveBlock.LIT))return 15;
                return 0;
            }))
            .state((provider, blockHolder) -> {
                var res = blockHolder.get().getRegistryName();
                var st = (StoveBlock) blockHolder.get();
                provider.getVariantBuilder(st).forAllStates(state -> {
                    var model = new ModelFile.UncheckedModelFile(new ResourceLocation(res.getNamespace(), "block/" + res.getPath() + (state.getValue(StoveBlock.LIT) ? "_on" : "")));
                    var ret = new ConfiguredModel(model, 0, ModelUtil.getRotY(state.getValue(BlockStateProperties.HORIZONTAL_FACING)), true);
                    return new ConfiguredModel[]{ret};
                });
            })
            .name("zh_cn","灶")
            .build();
    public static final BlockEntityTypeHolder<StoveBlockEntity> stoveBlockEntity=registry.blockEntity("stove",StoveBlockEntity::new)
            .withBlocks(stove)
            .build();
    public final static BlockHolder<LlandudnoBlock> llandudno=registry.block("llandudno",LlandudnoBlock::new)
            .state(ModelUtil.useModel())
            .propertie(BlockBehaviour.Properties::noOcclusion)
            .name("zh_cn","炼丹炉")
            .build();
    public final static BlockEntityTypeHolder<LlandudnoBlockEntity> llandudnoBlockEntity=registry.blockEntity("llandudno",LlandudnoBlockEntity::new)
            .withBlocks(llandudno)
            .build();
    public final static MenuHolder<LlandudnoMenu> llandudnoMenu=registry.menu("llandudno",LlandudnoMenu::new);

    public static void init() {
        registry.executeWhenClientSetup(()->{
            ItemBlockRenderTypes.setRenderLayer(llandudno.get(), RenderType.cutoutMipped());
            ItemBlockRenderTypes.setRenderLayer(impactAltar.get(), RenderType.cutoutMipped());
            ItemBlockRenderTypes.setRenderLayer(stove.get(), RenderType.cutoutMipped());
            MenuScreens.register(llandudnoMenu.get(),LlandudnoScreen::new);
        });
    }
}
